package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.system.entity.DapOperateLog;
import cn.com.bluemoon.daps.system.entity.DapSystemInfo;
import cn.com.bluemoon.daps.system.entity.DapSystemMenuCollect;
import cn.com.bluemoon.daps.system.mapper.DapSystemInfoMapper;
import cn.com.bluemoon.daps.system.mapper.DapSystemMenuCollectMapper;
import cn.com.bluemoon.daps.system.service.DapSystemInfoService;
import cn.com.bluemoon.daps.system.service.DapSystemMenuCollectService;
import cn.com.bluemoon.daps.system.service.DapSystemUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class DapSystemMenuCollectServiceImpl extends ServiceImpl<DapSystemMenuCollectMapper, DapSystemMenuCollect> implements DapSystemMenuCollectService {


    @Override
    public IPage<DapSystemMenuCollect> selectByPage(Integer pageSize, Integer pageNum, String keyWord) {

        LambdaQueryWrapper<DapSystemMenuCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DapSystemMenuCollect::getCreateBy, UserInfoHolder.getAccount())
                .and(StringUtils.isNotEmpty(keyWord), w -> w.like(DapSystemMenuCollect::getMenuName, keyWord)
                .or().like(DapSystemMenuCollect::getPath, keyWord).or().like(DapSystemMenuCollect::getDescription, keyWord))
                .orderByDesc(DapSystemMenuCollect::getCreateTime);
        IPage<DapSystemMenuCollect> page = this.getBaseMapper().selectPage(new Page<>(pageNum, pageSize), wrapper);
        return page;
    }

    @Override
    public Boolean menuCollect(DapSystemMenuCollect collect) {

        return this.save(collect);
    }

    @Override
    public ResultBean<String> deleteMenu(String id) {

        String account = UserInfoHolder.getAccount();
        List<DapSystemMenuCollect> collects = getBaseMapper().selectList(new LambdaQueryWrapper<DapSystemMenuCollect>().eq(DapSystemMenuCollect::getCreateBy, account));
        Set<String> collect = collects.stream().map(DapSystemMenuCollect::getId).collect(Collectors.toSet());
        if(collect.contains(id)){
            this.getBaseMapper().deleteById(id);
            return ResultBean.ok("删除成功");
        }
        return ResultBean.error("无权限删除");
    }
}
